package org.azdroid.kit.activitys;

import java.util.HashMap;

import android.os.Message;
import android.support.v4.app.FragmentActivity;

import com.umeng.analytics.MobclickAgent;

public abstract class BaseActivity extends FragmentActivity {

	private HashMap<String, String> caches = new HashMap<String, String>();
	private UiHnadler mUiHnadler;
	private boolean isDestroyUiHandler = true;

	protected String getCacheValue(String key) {
		return caches.get(key);
	}

	protected void putCache(String key, String value) {
		if (key != null) {
			caches.put(key, value);
		}
	}

	protected void removeCache(String key) {
		if (key != null) {
			caches.remove(key);
		}
	}

	private void clearCache() {
		caches.clear();
	}

	public final void setCreateUiHandler(boolean isCreate) {
		if (isCreate) {
			mUiHnadler = new UiHnadler(this);
		}
	}

	public final UiHnadler getUiHandler() {
		return mUiHnadler;
	}

	public final void sendUiMessage(Message msg) {
		UiHnadler h = getUiHandler();
		if (h == null) {
			h = new UiHnadler(this);
			// throw new NullPointerException(
			// "UiHnadler NULL,UiHnadler没有创建，必须  setCreateUiHandler(boolean isCreate)...");
		}
		h.sendMessage(msg);
	}

	public final void sendEmptyMessageDelayed(int what, long delayMillis) {
		UiHnadler h = getUiHandler();
		if (h == null) {
			h = new UiHnadler(this);
			// throw new NullPointerException(
			// "UiHnadler NULL,UiHnadler没有创建，必须  setCreateUiHandler(boolean isCreate)...");
		}
		h.sendEmptyMessageDelayed(what, delayMillis);
	}

	public final void sendUiMessage(int what) {
		UiHnadler h = mUiHnadler;
		if (h == null) {
			h = new UiHnadler(this);
			// throw new NullPointerException(
			// "UiHnadler NULL,UiHnadler没有创建，必须  setCreateUiHandler(boolean isCreate)...");
		}
		h.sendEmptyMessage(what);
	}

	public final Message obtainMessage(int what) {
		UiHnadler h = mUiHnadler;
		if (h == null) {
			h = new UiHnadler(this);
			// throw new NullPointerException(
			// "UiHnadler NULL,UiHnadler没有创建，必须  setCreateUiHandler(boolean isCreate)...");
		}
		return h.obtainMessage(what);
	}

	public final Message obtainMessage() {
		UiHnadler h = mUiHnadler;
		if (h == null) {
			h = new UiHnadler(this);
			// throw new NullPointerException(
			// "UiHnadler NULL,UiHnadler没有创建，必须  setCreateUiHandler(boolean isCreate)...");
		}
		return h.obtainMessage();
	}

	private void onDestroyUiHandler() {
		if (isDestroyUiHandler) {
			if (mUiHnadler != null) {
				mUiHnadler = null;
			}
		}
	}

	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		try {
			clearCache();
			onReleaseMemory();
			onDestroyUiHandler();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public boolean isDestroyUiHandler() {
		return isDestroyUiHandler;
	}

	public void setDestroyUiHandler(boolean isDestroyUiHandler) {
		this.isDestroyUiHandler = isDestroyUiHandler;
	}

	/**
	 * Activity中的Handler,主要负责网络请求后的数据异步同步操作，UI异步更新
	 * 
	 * @param msg
	 */
	protected void onHandleMessage(Message msg) {

	}

	protected void onReleaseMemory() {

	}

}
